// pages/book/book.js
const app = getApp()

Component({

  /**
   * 页面的初始数据
   */
  data: {
    schedule_days:[],
    dayVisible:false,
    schedules:[],
    selectedday:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  lifetimes: {
    attached: function(options) {
      // 在组件实例进入页面节点树时执行
      wx.request({
        url: app.globalData.url+'/wechat/student_schedule_days',
        data: {
          username: app.globalData.localuserinfo.username,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          if(res2.statusCode==200){
            console.log('student_schedule_days',res2.data)
            const result=res2.data.map(r=>{return{label:r,value:r}});
            console.log("result",result)
            this.setData({schedule_days:result})
          }
        },
        fail:()=>{
          console.log('fail')
        }
      })
    }
  },


  methods:{
    onClickPicker:function(){
      console.log('onClickPicker')
        this.setData({dayVisible:true})
    },
    onPickerCancel:function(){
      this.setData({dayVisible:false})
    },
    onPickDay:function(e){
      const day=e.detail.value
      this.setData({selectedday:day});
      this.getSchedulesByDay()
    },
    getSchedulesByDay:function(){
      wx.request({
        url: app.globalData.url+'/wechat/student_schedules',
        data: {
          username: app.globalData.localuserinfo.username,
          date:this.data.selectedday[0]
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          if(res2.statusCode==200){
            this.setData({schedules:res2.data})
          }
        },
        fail:()=>{
          console.log('fail')
        }
      })
    },
    book:function(e){
      const scheduleId=e.target.id;
      wx.request({
        url: app.globalData.url+`/schedule/syncId/${scheduleId}`,
        method: 'get',
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          const syncId=res.data.syncId;
          if(syncId<0){
            return  wx.showToast({
              title: '操作失败',
              icon: 'none',
              duration: 2000
            })
          }
          this.pick(syncId,scheduleId)
        },
        fail:()=>{
          console.log('fail')
        }
      })
    },
    unbook:function(e){
      const scheduleId=e.target.id
      wx.request({
        url: app.globalData.url+`/schedule/syncId/${scheduleId}`,
        method: 'get',
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          const syncId=res.data.syncId;
          if(syncId<0){
            return  wx.showToast({
              title: '操作失败',
              icon: 'none',
              duration: 2000
            })
          }
          this.unpick(syncId,scheduleId)
        },
        fail:()=>{
          console.log('fail')
        }
      })
    },
    pick:function(syncId,scheduleId){
      wx.request({
        url: app.globalData.url+'/schedule/pick',
        method: 'post',
        data:{
          syncId,
          scheduleId,
          username: app.globalData.localuserinfo.username,
        },
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const result=res2.data.result;
          if(result){
            this.getSchedulesByDay()
          }else{
            return  wx.showToast({
              title: '选课失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail:()=>{
          console.log('fail')
        }
      })
    },
    unpick:function(syncId,scheduleId){
      wx.request({
        url: app.globalData.url+'/schedule/unpick',
        method: 'post',
        data:{
          syncId,
          scheduleId,
          username: app.globalData.localuserinfo.username,
        },
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const result=res2.data.result;
          if(result){
            this.getSchedulesByDay()
          }else{
            return  wx.showToast({
              title: '取消选课失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail:()=>{
          console.log('fail')
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})